package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.*;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_UNION_WAR_USER_PATRONS_VEHICLE_SEND_LIST,desc = "玩家门客、珍兽派遣情况")
public class UnionWarUserPatronsVehicleSendCmd extends AbstractCommand {

    private int activityId;
    private int phase;
    private int session;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.PatronsVehicleSendListReqMsg reqMsg = UnionWarActivityServerProto.PatronsVehicleSendListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        phase = reqMsg.getPhase();
        session = reqMsg.getSession();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        UnionWar unionWar = UnionPhaseWarMgr.getUnionWarMap().get(activityId);
        if(unionWar == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!unionWar.isJoin(unionUid)){
            ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
            return;
        }
        long now = System.currentTimeMillis();

        UnionWarActivityServerProto.PatronsVehicleSendListRespMsg.Builder respMsg = UnionWarActivityServerProto.PatronsVehicleSendListRespMsg.newBuilder();
        UnionWarDispatchDetail detail = unionWar.getUnionWarDispatchDetailList(unionUid, phase, session);
        if(detail != null){
            UnionWarUserPatrons unionWarUserPatrons = detail.getPatronsMap().get(player.getUserId());
            if(unionWarUserPatrons != null) {
                respMsg.setGoodsId(unionWarUserPatrons.getGoodsId());
            }
        }

        //遍历派遣明细
        Map<Integer, List<String>> patronsSession = new HashMap<>();
        Map<String, List<String>> vehicleSession = new HashMap<>();
        Map<Integer, List<String>> fishSession = new HashMap<>();
        Map<Integer, List<String>> cricketSession = new HashMap<>();
        Map<Integer, Map<Integer, UnionWarDispatchDetail>> detailMap = unionWar.getUnionWarDispatchDetailList().get(unionUid);
        if(detailMap != null){
            for(Map.Entry<Integer, Map<Integer, UnionWarDispatchDetail>> phaseEntry : detailMap.entrySet()){
                for(Map.Entry<Integer, UnionWarDispatchDetail> sessionEntry : phaseEntry.getValue().entrySet()){
                    UnionWarUserPatrons userPatrons = sessionEntry.getValue().getPatronsMap().get(player.getUserId());
                    if(userPatrons != null){
                        for(Map.Entry<Integer, UnionWarUserPatronsDetail> patronsEntry : userPatrons.getPatrons().entrySet()){
                            if(!patronsSession.containsKey(patronsEntry.getKey())){
                                patronsSession.put(patronsEntry.getKey(), new ArrayList<>());
                            }
                            patronsSession.get(patronsEntry.getKey()).add(phaseEntry.getKey() + "-" + sessionEntry.getKey());
                            if(patronsEntry.getValue().getType() == 1){//伏兵
                                respMsg.addAmbush(patronsEntry.getKey());
                            }
                        }
                        for(Map.Entry<Integer, String> vehicleEntry : userPatrons.getVehicleUidList().entrySet()){
                            if(!vehicleSession.containsKey(vehicleEntry.getValue())){
                                vehicleSession.put(vehicleEntry.getValue(), new ArrayList<>());
                            }
                            vehicleSession.get(vehicleEntry.getValue()).add(phaseEntry.getKey() + "-" + sessionEntry.getKey());
                        }
                        for(Map.Entry<Integer, Integer> fishEntry : userPatrons.getFishIdList().entrySet()){
                            if(!fishSession.containsKey(fishEntry.getValue())){
                                fishSession.put(fishEntry.getValue(), new ArrayList<>());
                            }
                            fishSession.get(fishEntry.getValue()).add(phaseEntry.getKey() + "-" + sessionEntry.getKey());
                        }
                        for(Map.Entry<Integer, Integer> cricketEntry : userPatrons.getCricketIdList().entrySet()){
                            if(!cricketSession.containsKey(cricketEntry.getValue())){
                                cricketSession.put(cricketEntry.getValue(), new ArrayList<>());
                            }
                            cricketSession.get(cricketEntry.getValue()).add(phaseEntry.getKey() + "-" + sessionEntry.getKey());
                        }
                    }
                }
            }
        }

        //门客出战次数
        if(unionWar.getUserPatronsSendMap().containsKey(player.getUserId())){
            Map<Integer, UserPatronsSendTimes> patronsSendTimesMap = unionWar.getUserPatronsSendMap().get(player.getUserId());
            for(UserPatronsSendTimes patronsSendTimes : patronsSendTimesMap.values()){
                if(patronsSendTimes.getTimes() > 0){
                    UnionWarActivityServerProto.SendTimeDetailTemp.Builder patronsTemp = UnionWarActivityServerProto.SendTimeDetailTemp.newBuilder();
                    patronsTemp.setId(String.valueOf(patronsSendTimes.getPatronsId()));
                    patronsTemp.setTimes(patronsSendTimes.getTimes());
                    if(patronsSession.containsKey(patronsSendTimes.getPatronsId())){
                        patronsTemp.addAllPhaseSession(patronsSession.get(patronsSendTimes.getPatronsId()));
                    }
                    respMsg.addSendPatronsIdList(patronsTemp);
                }
            }
        }

        //珍兽出战次数
        if(unionWar.getUserVehicleSendMap().containsKey(player.getUserId())){
            Map<String, UserVehicleSendTimes> vehicleSendTimesMap = unionWar.getUserVehicleSendMap().get(player.getUserId());
            for(UserVehicleSendTimes vehicleSendTimes : vehicleSendTimesMap.values()){
                if(vehicleSendTimes.getTimes() > 0){
                    UnionWarActivityServerProto.SendTimeDetailTemp.Builder vehicleTemp = UnionWarActivityServerProto.SendTimeDetailTemp.newBuilder();
                    vehicleTemp.setId(vehicleSendTimes.getVehicleUid());
                    vehicleTemp.setTimes(vehicleSendTimes.getTimes());
                    if(vehicleSession.containsKey(vehicleSendTimes.getVehicleUid())){
                        vehicleTemp.addAllPhaseSession(vehicleSession.get(vehicleSendTimes.getVehicleUid()));
                    }
                    respMsg.addSendVehicleIdList(vehicleTemp);
                }
            }
        }

        //鱼类出战次数
        if(unionWar.getUserFishSendMap().containsKey(player.getUserId())){
            Map<Integer, UserFishSendTimes> fishSendTimesMap = unionWar.getUserFishSendMap().get(player.getUserId());
            for(UserFishSendTimes fishSendTimes : fishSendTimesMap.values()){
                if(fishSendTimes.getTimes() > 0){
                    UnionWarActivityServerProto.SendTimeDetailTemp.Builder fishTemp = UnionWarActivityServerProto.SendTimeDetailTemp.newBuilder();
                    fishTemp.setId(fishSendTimes.getFishId() + "");
                    fishTemp.setTimes(fishSendTimes.getTimes());
                    if(fishSession.containsKey(fishSendTimes.getFishId())){
                        fishTemp.addAllPhaseSession(fishSession.get(fishSendTimes.getFishId()));
                    }
                    respMsg.addSendFishIdList(fishTemp);
                }
            }
        }
        //昆虫类出战次数
        if(unionWar.getUserCricketSendMap().containsKey(player.getUserId())){
            Map<Integer, UserCricketSendTimes> cricketSendTimesMap = unionWar.getUserCricketSendMap().get(player.getUserId());
            for(UserCricketSendTimes cricketSendTimes : cricketSendTimesMap.values()){
                if(cricketSendTimes.getTimes() > 0){
                    UnionWarActivityServerProto.SendTimeDetailTemp.Builder cricketTemp = UnionWarActivityServerProto.SendTimeDetailTemp.newBuilder();
                    cricketTemp.setId(cricketSendTimes.getCricketId() + "");
                    cricketTemp.setTimes(cricketSendTimes.getTimes());
                    if(cricketSession.containsKey(cricketSendTimes.getCricketId())){
                        cricketTemp.addAllPhaseSession(cricketSession.get(cricketSendTimes.getCricketId()));
                    }
                    respMsg.addSendCricketIdList(cricketTemp);
                }
            }
        }
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_UNION_WAR_USER_PATRONS_VEHICLE_SEND_LIST, respMsg);
        log.info("PatronsVehicle select cost {}",  System.currentTimeMillis() - now);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionWarActivityServerProto.PatronsVehicleSendListRespMsg.Builder respMsg = UnionWarActivityServerProto.PatronsVehicleSendListRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_UNION_WAR_USER_PATRONS_VEHICLE_SEND_LIST, respMsg);
        }
    }
}
